﻿using System.Windows.Forms;

namespace CloudShot
{
  class HotkeyBox : TextBox
  {
    private bool _keySetted;
    private bool _controlPressed;
    private bool _shiftPressed;
    private bool _altPressed;

    protected override void OnKeyDown(KeyEventArgs e)
    {
      base.OnKeyDown(e);

      _keySetted = false;

      _controlPressed = (e.Modifiers & Keys.Control) != 0;
      _shiftPressed = (e.Modifiers & Keys.Shift) != 0;
      _altPressed = (e.Modifiers & Keys.Alt) != 0;

      Text = GetText();

      e.SuppressKeyPress = true;
    }

    protected override bool ProcessKeyEventArgs(ref Message msg)
    {
      var key = ((Keys)(int)msg.WParam);

      if (key != Keys.Menu && key != Keys.ControlKey && key != Keys.ShiftKey)
      {
        Text = GetText();
        Text += key.ToString();
        _keySetted = true;
      }

      if (!_keySetted)
        Text = string.Empty;

      return base.ProcessKeyEventArgs(ref msg);
    }

    private string GetText()
    {
      string result = string.Empty;

      if (_controlPressed)
        result += "Ctrl + ";

      if (_shiftPressed)
        result += "Shift + ";

      if (_altPressed)
        result += "Alt + ";

      return result;
    }
  }
}
